package com.atxyj.prototype.mode;

import org.springframework.util.ReflectionUtils;

import javax.swing.*;
import java.awt.*;

/**
 * 原型模式应用实例：【例1】用原型模式模拟“孙悟空”复制自己
 */
public class PrototypeDemo {
    public static void main(String[] args) {
        JFrame jf = new JFrame("原型模式测试");
        jf.setLayout(new GridLayout(1, 2));
        Container contentPane = jf.getContentPane();
        SunWuKong sunWuKong1 = new SunWuKong();
        contentPane.add(sunWuKong1);
        SunWuKong sunWuKong2 = (SunWuKong) sunWuKong1.clone();
        contentPane.add(sunWuKong2);
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

class SunWuKong extends JPanel implements Cloneable {

    private static final long serialVersionUID = 5543049531872119328L;

    public SunWuKong() {
        JLabel l1 = new JLabel(new ImageIcon("src/image-20210701154316417.png"));
        this.add(l1);
    }

    public Object clone() {
        SunWuKong w = null;
        try {
            w = (SunWuKong) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("复制失败！");
        }
        return w;
    }
}